<template>
  <el-checkbox-group
    v-model="checkboxValue"
    v-bind="$attrs"
    :size="size"
    :min="min"
    :max="max"
    @change="handleChange"
  >
    <component
      :is="checkboxComponent"
      v-for="option in normalizedOptions"
      :key="option.value"
      :label="option.value"
      :disabled="option.disabled"
      :border="border"
    >
      {{ option.label }}
    </component>
  </el-checkbox-group>
</template>

<script setup>
import { computed } from 'vue'
import { ElCheckboxGroup, ElCheckbox, ElCheckboxButton } from 'element-plus'

const props = defineProps({
  modelValue: {
    type: Array,
    default: () => []
  },
  options: {
    type: Array,
    default: () => []
  },
  size: String,
  border: Boolean,
  button: Boolean, // 是否使用按钮样式
  min: Number,
  max: Number
})

const emit = defineEmits(['update:modelValue', 'change'])

const checkboxValue = computed({
  get() {
    return props.modelValue || []
  },
  set(value) {
    emit('update:modelValue', value)
  }
})

// 标准化选项格式
const normalizedOptions = computed(() => {
  return props.options.map(option => {
    if (typeof option === 'string' || typeof option === 'number') {
      return {
        label: String(option),
        value: option,
        disabled: false
      }
    }
    return {
      label: option.label || String(option.value),
      value: option.value,
      disabled: option.disabled || false
    }
  })
})

const checkboxComponent = computed(() => {
  return props.button ? ElCheckboxButton : ElCheckbox
})

const handleChange = (value) => {
  emit('change', value)
}
</script>